<template>
  <ul>
    <li>
      <div>商品名称关键字</div>
      <div class="zy">
      <el-input placeholder="请输入商品关键字" v-model="keyword"></el-input>
    </div>
      <div><el-button type="primary" @click="searchName">搜索</el-button></div>
    </li>
    <li>
      <div class="block">
        <span class="demonstration">商品名称和规格</span>
        <el-cascader
            v-model="value"
            :options="options"
            @change="handleChange"></el-cascader>
      </div>
    </li>
    <li>
      <div>起始日期</div>
      <div class="block zy">
      <el-date-picker
          v-model="goStart"
          type="datetime"
          placeholder="选择开始日期时间">
      </el-date-picker>
    </div></li>
    <li>
      <div>结束日期</div>
      <div class="block zy">
        <el-date-picker
            v-model="beOver"
            type="datetime"
            placeholder="选择结束日期时间">
        </el-date-picker>
      </div>
    </li>
    <li>
      <div>秒杀价格</div>
      <div class="zy"><el-input v-model="money" placeholder="请输入秒杀价格"></el-input></div>
    </li>
    <li>
      <div>秒杀库存</div>
      <div class="zy"><el-input v-model="stock" placeholder="请输入秒杀库存"></el-input></div>
    </li>
    <li>
      <div>秒杀规则</div>
      <div class="zy">
        <el-input
          type="textarea"
          placeholder="请输入秒杀"
          v-model="role">
      </el-input>
      </div>
    </li>
    <li><el-button type="primary" @click="addSecKill">确定</el-button></li>
  </ul>
</template>

<script>
export default {
name: "add",
  inject:['reload'],
  data(){
  return {
    keyword:'',
    shopName:'',
    goStart:'',
    beOver:'',
    money:'',
    stock:'',
    role:'',
    value: [],
    options: [],
  }
  },
  methods:{
  searchName(){
    this.$axios.post('/api/goods/searchGoodsFormatByGoodsName?keyWords='+this.keyword).then((res)=>{
      let list=res.data.data;
      if(list.length>0){
        console.log(list);
        list.forEach((item)=>{
          this.options.push(item);
        });
        list.forEach((item,index)=>{
          this.options[index].value=item.goodsId;
          this.options[index].label=item.goodsName;
          this.options[index].children=item.goodsFormatList;
          this.options[index].children.forEach((tem)=>{
            tem.value=tem.goodsFormatId;
            tem.label=tem.goodsFormatName;
          })
        });
      }else {
        this.$message('商品中没有你想要的数据');
      }
    })
  },
    handleChange(value) {
      console.log(value);
    },
    addSecKill:function (){
      this.goStart=this.goStart.getFullYear() + '-' + (this.goStart.getMonth() + 1) + '-' + this.goStart.getDate() + ' ' + this.goStart.getHours() + ':' + this.goStart.getMinutes() + ':' + this.goStart.getSeconds();
      this.beOver=this.beOver.getFullYear() + '-' + (this.beOver.getMonth() + 1) + '-' + this.beOver.getDate() + ' ' + this.beOver.getHours() + ':' + this.beOver.getMinutes() + ':' + this.beOver.getSeconds();
      console.log(this.goStart);
      console.log(this.beOver);
      let goAndBe= this.dDate(this.goStart,this.beOver);
      if(goAndBe>0){
        this.$axios.post('/api/flash/add',{goodsId:this.value[1],flashStartTime:this.goStart,flashEndTime:this.beOver,flashPrice:this.money,flashStock:this.stock,flashDesc:this.role}).then((res)=>{
          console.log(res.data);
          if(res.data.code==200){
            this.$message({
              message: res.data.msg,
              type: 'success'
            });
          }else if(res.data.code==1001){
            this.$message.error(res.data.msg);
          }
          this.reload();
        })
      }else if(goAndBe<0){
        this.$message.error('结束时间大于起始时间请重新输入');
      }
    },
    dDate(DateOne, DateTwo) {
      //两个时间的天数差(DateOne 较小的时间, DateTwo 较大的时间，日期类型：2016-06-16)
      var OneMonth = DateOne.substring(5, DateOne.lastIndexOf('-'));
      var OneDay = DateOne.substring(7, 9);
      var OneYear = DateOne.substring(0, DateOne.indexOf('-'));
      var TwoMonth = DateTwo.substring(5, DateTwo.lastIndexOf('-'));
      var TwoDay = DateTwo.substring(7, 9);
      var TwoYear = DateTwo.substring(0, DateTwo.indexOf('-'));
      var cha2 = -(Date.parse(OneYear+'/'+OneMonth +'/'+ OneDay) - Date.parse(TwoYear + '/' + TwoMonth + '/' + TwoDay));
      return cha2;
      //return Math.abs(cha); //返回差值绝对值
    },
  }
}
</script>

<style scoped>
ul li{
  list-style-type: none;
  display: flex;
  justify-content: flex-start;
  align-items: center;
  height: 100px;
}
.zy{
  margin-left: 20px;
}
ul>li:nth-child(8)>div:last-child{
  width: 300px;
}
</style>